import { ListStore } from '@/store';
import {
  INITIAL_FORM_QUERY_VALUE,
  LIST_NAME,
  LOAD_PRICE_BASE_GOODS_LIST_REQUEST_TASK_NAME,
  INITIAL_FORM_VALUE,
} from '@/views/price-base-goods/constants';
import { loadListStore } from './api';
import { message } from '@/utils/util';

export default class Store extends ListStore {
  formQueryValue = { ...INITIAL_FORM_QUERY_VALUE };

  querying = false;

  visible = false;

  // 当前编辑商品参数 - 用于每次打开Modal时作为其表单初始值传入
  formValue = { ...INITIAL_FORM_VALUE };

  importVisible = false;

  // 加载列表
  loadDataList = async ({ loadAction = 'reset', pageSize: inputPageSize, pageNum: inputPageNum } = {}) => {
    const { topChannelCode } = this.formQueryValue;
    if (!topChannelCode) {
      message('提示', '请选择顶级渠道', 'warning');
      return;
    }
    await this.loadList({
      requestTaskName: LOAD_PRICE_BASE_GOODS_LIST_REQUEST_TASK_NAME,
      listName: LIST_NAME,
      loadAction,
      pageSize: inputPageSize,
      pageNum: inputPageNum,
      loadDataFunc: async ({ pageSize, pageNum }) => {
        const result = await loadListStore(this.formQueryValue, { pageSize, pageNum });
        let { rows: data = [], total = 0 } = result || {};
        return { data, total };
      },
    });
  };
  // 查询门店设置
  queryList = value => {
    this.formQueryValue = value;
    this.loadDataList({ loadAction: 'pageNumChange', pageNum: 1 });
  };

  // 当前页变更
  pageNumChange = pageNum => {
    this.loadDataList({ loadAction: 'pageNumChange', pageNum });
  };

  // 每页条数变更
  pageSizeChange = pageSize => {
    this.loadDataList({ loadAction: 'pageSizeChange', pageSize });
  };

  resetTable = value => {
    this.formQueryValue = { ...this.formQueryValue, topChannelCode: value };
    this.loadDataList({ loadAction: 'reset' });
  };
  //关闭新增编辑对话框
  onClose = () => {
    this.visible = false;
    this.formValue = { ...INITIAL_FORM_VALUE };
  };
  //打开新增编辑对话框
  onOpenEditModal = value => {
    this.visible = true;
    if (value) {
      this.formValue = value;
    }
  };
  //打开导入对话框
  onOpenImportModal = () => {
    this.importVisible = true;
  };
  //关闭导入对话框
  onCloseImport = () => {
    this.importVisible = false;
  };

  // 初始加载数据
  init = async () => {
    await this.loadDataList({ loadAction: 'reset' });
  };
}
